package com.soundai.skill.helloworld.Controller;

import com.amazon.ask.dispatcher.request.handler.HandlerInput;
import com.amazon.ask.model.Slot;
import com.amazon.ask.model.canfulfill.CanFulfillIntentRequest;
import com.soundai.skill.helloworld.HttpHelper.HttpClientHelper;
import com.soundai.skill.helloworld.Service.ShoppingService;
import com.soundai.skill.helloworld.Service.WisdomHomeService;
import com.soundai.skill.helloworld.baiduApi.ApiNlpDataHelper;
import com.soundai.skill.helloworld.config.uriConfig;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.json.JSONException;

/**
 *
 * TODO : 处理NLU发送过来的请求内容
 * BY  : Yanpeixin
 * Date:2019-08-01
 *
 */
public class NLPController {

    public static String NLP(HandlerInput handlerInput, CanFulfillIntentRequest canFulfillIntentRequest){
        /*try {
            JSONObject returnMsg = new JSONObject();

            Slot slot = canFulfillIntentRequest.getIntent().getSlots().get("SlotName");
            JSONObject objV = JSONObject.fromObject(slot.getValue());
            String speek = objV.get("query").toString();

            //把分词结果进行数据检索，找出符合的领域及相关动作
            String doGetUrl = String.format(uriConfig.areaActionByWordsUrl,  ApiNlpDataHelper.getWords(speek,""));
            String dtAreaAction = HttpClientHelper.doGet(doGetUrl);
            //JSONArray arrAreaAction = JSONArray.fromObject(dtAreaAction);
            //把分词结果进行数据检索，找出符合的领域及相关动作
            String area = getAreaAction(dtAreaAction,"k"); //领域
            String action = getAreaAction(dtAreaAction,"v"); //动作
            String weight = getAreaAction(dtAreaAction,"weight");//权重

            switch (area){
                case "shopping":
                    switch (action){
                        case "queryProduct": //商品查询
                           String productList = ShoppingService.productInfo(speek);
                            break;
                        case "waitConfrim": //等待确认
                            String waitConfrimData = ShoppingService.waitConfrim(speek);
                            break;
                        case  "order": //订单确认

                            break;
                    }
                    break;
                case "device":
                    //returnMsg = WisdomHomeService.requestSound(handlerInput, speek);
                    break;
                case "music":
                    break;
                    default:
                        break;
            }


        } catch (JSONException e) {
            e.printStackTrace();
        }*/
        return "";
    }


    /**
     * 识别区域+动作+权重
     * @param dtAreaAction 领域动作结果集
     * @param keys  取值的字段
     * @return 根据取值字段，返回对应的区域+动作+权重
     */
    public static String getAreaAction(String dtAreaAction,String keys){
        String returnMsg = "";
        JSONArray arrAreaAction = JSONArray.fromObject(dtAreaAction);
        for(int i=0;i<arrAreaAction.size();i++){
            JSONObject joAreaAction = arrAreaAction.getJSONObject(i);
            /*String area = joAreaAction.get("k").toString();
            String action = joAreaAction.get("v").toString();
            String weight = joAreaAction.get("weight").toString();*/
            returnMsg = joAreaAction.get(keys).toString();
        }
        return returnMsg;
    }



}
